home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_6 / difflim.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  930 b   |  34 lines  |  [MATF/MATL]

  1. function [H,D,E,n] = difflim(f,x,toler)
  2. % [H,D,E,n] = difflim(f,x,toler)
  3. % Numerical approximation for f'(x).
  4. % The method employed is the limit process.
  5. % f is the function, input.
  6. % x is differentiation point, input.
  7. % H is the vector of step sizes, output.
  8. % D is the vector of approximate derivatives, output.
  9. % E is the vector of error bounds, output.
  10. % n is the coordinate of the "best approximation", output.
  11. h = 1;
  12. max1 = 15;
  13. H(1) = h;
  14. D(1) = (feval(f,x+h) - feval(f,x-h))/(2*h);
  15. E(1) = 0;
  16. R(1) = 0;
  17. for n = 1:2,
  18.   h = h/10;
  19.   H(n+1) = h;
  20.   D(n+1) = (feval(f,x+h) - feval(f,x-h))/(2*h);
  21.   E(n+1) = abs(D(n+1) - D(n));
  22.   R(n+1) = 2*E(n+1)*(abs(D(n+1)) + abs(D(n)) + eps);
  23. end
  24. n = 2;
  25. while ((E(n)>E(n+1)) & (R(n)>toler)) & n<max1
  26.   h = h/10;
  27.   H(n+2) = h;
  28.   D(n+2) = (feval(f,x+h) - feval(f,x-h))/(2*h);
  29.   E(n+2) = abs(D(n+2) - D(n+1));
  30.   R(n+2) = 2*E(n+2)*(abs(D(n+2)) + abs(D(n+1)) + eps);
  31.   n = n+1;
  32. end
  33. n = length(D)-1;
  34.